package com.dlwx.parttimetalent.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.CompoundButton;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;

import com.dlwx.parttimetalent.R;
import com.dlwx.parttimetalent.bean.OrderRecBean;
import com.dlwx.parttimetalent.inter.DialogFilerInter;

import java.util.List;

/**
 * Created by Administrator on 2017/11/24.
 */

public class DialogHyyrExpandAdapter extends BaseExpandableListAdapter {
    private final LayoutInflater mInflater;
    public List<OrderRecBean.BodyBean.ChildBeanX> group ;
    private Context context;
    private DialogFilerInter inter;

    public DialogHyyrExpandAdapter(List<OrderRecBean.BodyBean.ChildBeanX> group, Context context, DialogFilerInter inter) {
        this.group = group;
        this.context = context;
        this.inter=inter;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public int getGroupCount() {
        return group.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        List<OrderRecBean.BodyBean.ChildBeanX.ChildBean> childList = group.get(groupPosition).getChild();
        if(childList==null){
            return 0;
        }
        return childList.size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return group.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        List<OrderRecBean.BodyBean.ChildBeanX.ChildBean> childList = group.get(groupPosition).getChild();
        if(childList==null){
            return null;
        }
        return childList.get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(final int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder=new ViewHolder();
            convertView = mInflater.inflate(R.layout.expand_group, null);
            viewHolder.tvTitle= convertView.findViewById(R.id.tv_title);
            viewHolder.swStatus1=convertView.findViewById(R.id.sw_group);
            viewHolder.imageArrow=convertView.findViewById(R.id.image_arrow);
            convertView.setTag(viewHolder);
        }else{
            viewHolder= (ViewHolder) convertView.getTag();
        }
        if(isExpanded){
            viewHolder.imageArrow.setBackgroundResource(R.mipmap.arrow_down);
        }else{
            viewHolder.imageArrow.setBackgroundResource(R.mipmap.arrow_top);
        }
        viewHolder.tvTitle.setText(group.get(groupPosition).getChildname());
        viewHolder.swStatus1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            }
        });
        return convertView;
    }

    @Override
    public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        ChildViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder=new ChildViewHolder();
            convertView = mInflater.inflate(R.layout.expand_child1, null);
            viewHolder.tvContent=convertView.findViewById(R.id.tv_content);
            viewHolder.swStatus2=convertView.findViewById(R.id.sw_status);
            convertView.setTag(viewHolder);
        }else{
            viewHolder= (ChildViewHolder)convertView.getTag();
        }
        List<OrderRecBean.BodyBean.ChildBeanX.ChildBean> childList = group.get(groupPosition).getChild();
        if(childList!=null&&!childList.isEmpty()){
            OrderRecBean.BodyBean.ChildBeanX.ChildBean childBean = childList.get(childPosition);
            viewHolder.tvContent.setText(childBean.getChildname());
        }
        viewHolder.swStatus2.setVisibility(View.GONE);
        return convertView;
    }
    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    private static class ViewHolder
    {
        TextView tvTitle;
        Switch swStatus1,swStatus2;
        ImageView imageArrow;
        boolean isOpen;

    }
    private static class ChildViewHolder{
        TextView tvContent;
        Switch swStatus2;
        ExpandableListView expandListview;

    }
}
